package J4_29;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class test {
    public static int maxLength (int[] arr) {
        if (arr.length==0)return 0;
        int ret = 1;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(arr[0],0);
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (map.containsKey(arr[j])) {
                    ret = Math.max(ret,j-i);
                    int tmp = map.get(arr[j]);
                    for (int k = i; k <= tmp; k++) {
                        map.remove(arr[k]);
                    }
                    i = tmp+1;
                }
                if (j == arr.length-1) {
                    ret = Math.max(ret,j - i + 1);
                    return ret;
                }
                map.put(arr[j],j);
            }
        }
        return ret;
    }
    public static void canRearrange(String str) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1);
        }
        int len = 0;
        char a = ' ';
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if (entry.getValue() > len) {
                len = entry.getValue();
                a = entry.getKey();
            }
        }
        int sum = 0, count = -1;
        StringBuilder s = new StringBuilder();
        StringBuilder s2 = new StringBuilder();
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if (entry.getKey() != a) {
                sum += entry.getValue();

            }
            if (entry.getValue() == len) {
                s2.append(entry.getKey());
                count++;
            } else {
                for (int i = 0; i < entry.getValue(); i++) {
                    s.append(entry.getKey());
                }
            }
        }
        if (sum >= len - 1)  {
            System.out.println("yes");
            int n = (sum - count * len) / (len - 1);
            int m = (sum - count * len) % (len-1);
            StringBuilder s1 = new StringBuilder();
            for (int i = 0; i < len-1; i++) {
                s1.append(s2);
                if (m > 0) {
                    for (int j = 0; j < n + 1; j++) {
                        s1.append(s.charAt(i + j * (len -1)));
                    }
                    m--;
                } else {
                    for (int j = 0; j < n; j++) {
                        s1.append(s.charAt(i + j * (len -1)));
                    }
                }
            }
            s1.append(s2);
            System.out.println(s1);
        } else {
            System.out.println("no");
        }
    }

    public static void main(String[] args) {
        String str = "aaaaabbcc";
        canRearrange(str);
    }
}
